package com.sinopec.basemodule.dialog;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sinopec.basemodule.R;
import com.sinopec.basemodule.widget.popupwindow.BasePopup;

/**
 * @author lwj
 * @ClassName: TipsPopupWindow
 * @Description: 提示弹窗
 */
public class TipsPopupWindow extends BasePopup<TipsPopupWindow> {
    private Context mContext;
    private OnSelectListener listener;
    //标题
    private TextView tv_title;
    //内容
    private TextView tv_content;
    //标题
    private String title;
    //内容
    private String content;
    //确认按钮
    private TextView tv_confirm;

    public static TipsPopupWindow create(Context context, String title, String content, OnSelectListener listener) {
        return new TipsPopupWindow(context, title, content, listener);
    }

    private TipsPopupWindow(Context context, String title, String content, OnSelectListener listener) {
        this.listener = listener;
        this.mContext = context;
        this.title = title;
        this.content = content;
        setContext(context);
    }

    @Override
    protected void initAttributes() {
        setContentView(R.layout.tips_popupwindow);
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        //设置弹窗宽高比例IntEKt.dp2px(370)
        this.setWidth((int) (d.widthPixels * 0.35));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusAndOutsideEnable(false)
                .setBackgroundDimEnable(true)
                .setNeedReMeasureWH(true)
                .setDimValue(0.5f);

//        setContentView(R.layout.tips_popupwindow, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        setFocusAndOutsideEnable(false)
//                .setBackgroundDimEnable(true)
//                .setNeedReMeasureWH(true)
//                .setDimValue(0.5f);
    }

    @Override
    protected void initViews(View view, TipsPopupWindow basePopup) {
        //标题
        tv_title = view.findViewById(R.id.tv_title);
        if(!TextUtils.isEmpty(title)){
            tv_title.setVisibility(View.VISIBLE);
            tv_title.setText(Html.fromHtml(title));
        }else{
            tv_title.setVisibility(View.GONE);
        }
        //内容
        tv_content = view.findViewById(R.id.tv_content);
        if(!TextUtils.isEmpty(content)){
            tv_content.setVisibility(View.VISIBLE);
            tv_content.setText(Html.fromHtml(content));
        }else{
            tv_content.setVisibility(View.GONE);
        }
        //确认按钮
        tv_confirm = view.findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onConfirm();
                dismiss();//关闭弹窗
            }
        });
    }

    public interface OnSelectListener {
        /**
         * 确认
         */
        void onConfirm();
    }
}
